package com.dream.inspect.modules.business.controller;

import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.modules.business.entity.SuperviseRecordEntity;
import com.dream.inspect.modules.business.service.SuperviseRecordService;
import com.dream.inspect.modules.dream.pojo.SysUserFormMap;
import com.dream.inspect.modules.sys.controller.AbstractController;



/**
 * 督察记录表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 17:04:52
 */
@RestController
@CrossOrigin
@RequestMapping("business/superviserecord")
public class SuperviseRecordController extends AbstractController {
    @Autowired
    private SuperviseRecordService superviseRecordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:superviserecord:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = superviseRecordService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listMultiple")
    @RequiresPermissions("business:listMultiple")
    public R listMultiple(@RequestParam Map<String, String> params){
        SysUserFormMap user = getSessionUser();
        params.put("dcdwId", String.valueOf(user.getLong("deptId")));
        params.put("v2", user.get("roleId").toString());
        PageUtils page = superviseRecordService.selectSuperviseRecordListPage(params);

        return R.ok().put("page", page).put("user", user);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:superviserecord:info")
    public R info(@PathVariable("id") Long id){
        SuperviseRecordEntity superviseRecord = superviseRecordService.selectById(id);

        return R.ok().put("superviseRecord", superviseRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:superviserecord:save")
    public R save(@RequestBody SuperviseRecordEntity superviseRecord){
        superviseRecordService.insert(superviseRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:superviserecord:update")
    public R update(@RequestBody SuperviseRecordEntity superviseRecord){
        ValidatorUtils.validateEntity(superviseRecord);
        superviseRecordService.updateAllColumnById(superviseRecord);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:superviserecord:delete")
    public R delete(@RequestBody Long[] ids){
        superviseRecordService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 督察记录详情信息
     */
    @RequestMapping("/detailInfo")
    //@RequiresPermissions("business:superviserecord:info")
    public R detailInfo(@RequestParam Map<String, String> params){
//        params.put("id", "394");
        SuperviseRecordEntity superviseRecord = superviseRecordService.selectSuperviseRecordByIdDetail(params);
        if (superviseRecord != null) {
            String result = "0";
            if(superviseRecord.getQuestionNum() != 0 ){
                NumberFormat numberFormat = NumberFormat.getInstance();
                // 设置精确到小数点后2位
                numberFormat.setMaximumFractionDigits(2);

                result = numberFormat.format((float) superviseRecord.getQuestionModifyNum() / (float) superviseRecord.getQuestionNum() * 100);
            }
            superviseRecord.setPrecent(result);
        }

        return R.ok().put("superviseRecord", superviseRecord);
    }

}
